﻿
@{
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using yiran.Areas.Admin.Models.Entity
@model List<yiran.Models.Entity.yr_banner>
<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a>首页</a>
            <a class="p_name"></a>
            <a>
                <cite class="c_name"></cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    @Html.AntiForgeryToken();
                    <div class="layui-card-body layui-table-body layui-table-main">
                        <div class="layui-tab layui-tab-card">
                            <ul class="layui-tab-title">
                                <li class="layui-this ">电脑Banner管理</li>
                                <li>手机Banner管理</li>
                            </ul>
                            <div class="layui-tab-content" style="overflow-x:auto">
                                <div class="layui-tab-item layui-show">
                                    <div class="layui-card-header" style="padding-bottom:10px;margin-bottom:15px;padding-left:0">
                                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "delbanner").Any())
                                        {
                                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                                        }
                                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "addbanner").Any())
                                        {
                                            <button class="layui-btn" onclick="xadmin.open('添加banner','/Admin/HomePage/AddBanner?type=1',700,700)"><i class="layui-icon"></i>添加</button>
                                        }
                                    </div>
                                    <table class="layui-table layui-form">
                                        <thead>
                                            <tr>
                                                <th>
                                                    <input title="全选" type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                                                </th>
                                                <th>图片</th>
                                                <th>外链</th>
                                                <th>排序</th>
                                                <th>备注</th>
                                                <th>状态</th>
                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>

                                            @if (Model != null)
                                            {
                                                var i = 0;
                                                foreach (var m in Model.Where(c => c.type == 1))
                                                {
                                                <tr>
                                                    <td>
                                                        <input type="checkbox" name="id" title="@(i=i+1)" data-type="@m.type" value="@m.id" lay-skin="primary">
                                                    </td>
                                                    <td><img class="showImg" style="cursor:pointer;display:block;margin:auto;max-width:220px" src="@m.image" alt="Alternate Text" /></td>
                                                    <td><a href="@m.href" target="_blank" style="text-decoration:underline">@m.href</a></td>
                                                    <td>
                                                        <input onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" type="text" value="@m.sork" data-value="@m.sork" data-id="@m.id" data-type="@m.type" onblur="setSork(this)" id="sork" name="sork" required="" lay-verify="required" autocomplete="off" class="layui-input" />
                                                        <p class="x-red">数值越大越靠前</p>
                                                    </td>
                                                    <td>@m.remark</td>
                                                    <td align="center"  data-id="@m.id">
                                                        <input type="checkbox"  data-id="@m.id" @(m.Static == 1 ? "checked" : "") name="Static" value="@m.Static" lay-skin="switch" lay-text="ON|OFF" lay-filter="switchTest">
                                                    </td>
                                                    <td class="td-manage">
                                                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "editbanner").Any())
                                                        {
                                                            <a title="编辑" onclick="xadmin.open('编辑','@Url.Action("Editbanner","HomePage",new { area = "Admin" , id = @m.id})',700,700)" href="javascript:;">
                                                                <i class="layui-icon">&#xe642;</i>
                                                            </a>
                                                        }
                                                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "delbanner").Any())
                                                        {
                                                            <a title="删除" onclick="member_del(this,'@m.id','@m.type')" href="javascript:;">
                                                                <i class="layui-icon">&#xe640;</i>
                                                            </a>
                                                        }

                                                    </td>
                                                </tr>
                                                }
                                            }
                                            else
                                            {
                                                <tr>
                                                    <td colspan="9" align="center">暂无数据</td>
                                                </tr>
                                            }
                                        </tbody>
                                    </table>
                                </div>

                                <div class="layui-tab-item">
                                    <div class="layui-card-header" style="padding-bottom:10px;margin-bottom:15px;padding-left:0">
                                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "delbanner").Any())
                                        {
                                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                                        }
                                        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "addbanner").Any())
                                        {
                                            <button class="layui-btn" onclick="xadmin.open('添加banner','/Admin/HomePage/AddBanner?type=2',700,700)"><i class="layui-icon"></i>添加</button>
                                        }
                                    </div>
                                    <table class="layui-table layui-form">
                                        <thead>
                                            <tr>
                                                <th>
                                                    <input title="全选" type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                                                </th>
                                                <th>图片</th>
                                                <th>外链</th>
                                                <th>排序</th>
                                                <th>备注</th>
                                                <th>状态</th>
                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @if (Model != null)
                                            {
                                                var i = 0;
                                                foreach (var m in Model.Where(c => c.type == 2))
                                                {
                                                    <tr>
                                                        <td>
                                                            <input type="checkbox" name="id" title="@(i=i+1)" data-type="@m.type" value="@m.id" lay-skin="primary">
                                                        </td>
                                                        <td><img class="showImg" style="cursor:pointer;display:block;margin:auto;max-width:220px" src="@m.image" alt="Alternate Text" /></td>
                                                        <td><a href="@m.href" target="_blank" style="text-decoration:underline">@m.href</a></td>
                                                        <td>
                                                            <input @((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "delbanner").Any() ? "":"disabled")  onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" type="text" value="@m.sork" data-value="@m.sork" data-id="@m.id" data-type="@m.type" onblur="setSork(this)" id="sork" name="sork" required="" lay-verify="required" autocomplete="off" class="layui-input" />
                                                            <p class="x-red">数值越大越靠前</p>
                                                        </td>
                                                        <td>@m.remark</td> 
                                                        <td align="center" data-id="@m.id">
                                                            <input type="checkbox" @(m.Static == 1 ? "checked" : "") name="Static"  data-id="@m.id" value="@m.Static" lay-skin="switch" lay-text="ON|OFF" lay-filter="switchTest">
                                                        </td>
                                                        <td class="td-manage">
                                                            @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "editbanner").Any())
                                                            {
                                                                <a title="编辑" onclick="xadmin.open('编辑','@Url.Action("Editbanner","HomePage",new { area = "Admin" , id = @m.id})',700,700)" href="javascript:;">
                                                                    <i class="layui-icon">&#xe642;</i>
                                                                </a>
                                                            }
                                                            @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "delbanner").Any())
                                                            {
                                                                <a title="删除" onclick="member_del(this,'@m.id','@m.type')" href="javascript:;">
                                                                    <i class="layui-icon">&#xe640;</i>
                                                                </a>
                                                            }
                                                        </td>
                                                    </tr>
                                                }
                                            }
                                            else
                                            {
                                                <tr>
                                                    <td colspan="9" align="center">暂无数据</td>
                                                </tr>
                                            }
                                        </tbody>
                                    </table>
                                </div>

                            </div>
                        </div>

                    </div>
                    <div class="layui-card-body ">

                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    layui.use(['laydate', 'form','element'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
         @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "editbanner").Any())
         {
              <text>
             form.on('switch(switchTest)', function(data){
             if (!this.checked) {
                     layer.tips('关闭后前台将不展示本数据', data.othis)
             }
                 var Static = this.checked ? '1' : '0';
                 $("input[name=Static]").val(Static)
                 $.ajax({
                 url: '@Url.Action("Staticbanner","HomePage",new { area="Admin"})',
                data: {id:$(data.othis).siblings("input").attr("data-id"),Static:Static,__RequestVerificationToken:$("input[name=__RequestVerificationToken]").val()},
			            type: "post",
			            async:true,
			            dataType: 'json',
                        success: function (data) {
                         if (data.code == 200) {
                             //发异步，把数据提交给php
                             layer.alert(data.result, {
                             icon: 6
                                });
                         } else {
                             layer.msg(data.result, { icon: 0, time: 2000 });
                         }
                     },
			            error:function(xhr,errorText,errorType){
                         alert("error");
                     },
			            complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
                         if(status=='timeout'){
                             ajax.abort();
                             alert("请求超时");
                         }
                     }
                 })
        });
            </text>
         }


        // 监听全选
        form.on('checkbox(checkall)', function(data){
          if(data.elem.checked){
            $('.layui-show tbody input[name = id]').prop('checked',true);
          }else{
            $('.layui-show tbody input[name = id]').prop('checked',false);
          }
          form.render('checkbox');
        });

    });

    @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "delbanner").Any())
    {
        <text>
            /*用户-删除*/
            function member_del(obj,id,type){
                  layer.confirm('确认要删除吗？',function(index){
                      //发异步删除数据
                      $.ajax({
                        url: '@Url.Content("/Admin/Homepage/Delbanner")',
                        type: 'POST',
                        data: { id: id},
                        dataType: 'json',
                        async: true,
                        success: function (data, status, xhr) {
                            if (data.code == 200) {
                                layer.msg(data.result, {
                                    icon: 1,
                                    time: 2000
                                }, function(){
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.result, { icon: 2 });
                            }
                        }
			          });
                      //$(obj).parents("tr").remove();
                      //layer.msg('已删除!',{icon:1,time:1000});
                  });
    }
    function delAll (argument) {
        var ids = [];

        // 获取选中的id
        $('.layui-show tbody input[name = id]').each(function(index, el) {
            if($(this).prop('checked')){
                ids.push($(this).val())
            }

        });
         if (ids.length @Html.Raw("<")= 0) {
             layer.msg("请选择要删除的行", { icon: 2, time: 2000 });
             return false;
         }
         layer.confirm('确认要删除吗？', function (index) {
             $.ajax({
                 url: '@Url.Content("/Admin/Homepage/Delbanner")',
                 type: 'POST',
                 data: { id:ids },
				dataType: 'json',
				async: true,
                success: function (data, status, xhr) {
                    if (data.code == 200) {
                        layer.msg(data.result, {
                            icon: 1,
                            time: 2000
                        }, function(){
                            location.reload();
                        });
                    } else {
                        layer.msg(data.result, { icon: 2 });
                    }
				}
			});
        });
    }
        </text>
    }

    $(".showImg").click(function () {
        var content = '<div style="height: 100%; overflow: hidden !important;">' +
			'<img src="' + $(this).attr("src") + ' " style="width: 100%; height: 100%;" />' +
		 '</div>';
        layer.open({
		    type: 1,
		    title: false,
		    shadeClose: true,
			closeBtn: 0,
		    area: '60%', //宽高
		    content: content
		});
    })
     @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "editbanner").Any()) {
        <text>
        function setSork(obj) {
        var sork = $(obj).val();
        var id = $(obj).attr("data-id")
        if (sork != $(obj).attr("data-value")) {
            layer.confirm('是否更改排序？', function (index) {
                var data = {
                    id: id,
                    sork: sork,
                    __RequestVerificationToken:$("input[name=__RequestVerificationToken]").val()
                }
                $.ajax({
					url:'@Url.Content("/Admin/Homepage/SetSork")',
					type: 'POST',
					data: data,
					dataType: 'json',
					async: true,
                    success: function (data, status, xhr) {
                        if (data.code == 200) {
                            layer.msg(data.result, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                location.reload();
                            });
                        } else {
                            layer.msg(data.result, { icon: 2 });
                        }
					}
				});

            }, function () {
                $(obj).val($(obj).attr("data-value"));
            });
        }
    }
    </text>
     }



</script>
